home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / xdme_1.84_src.lha / XDME / Util / Var / subs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-22  |  1.3 KB  |  70 lines

  1.  
  2.  
  3. char bras[] = "[{(<`«\"";
  4. char kets[] = "]})>'»\"";
  5.  
  6. char *openbrackets = bras;
  7. char *closebrackets = kets;
  8.  
  9. char match_of (char bra)
  10. {
  11.     int i;
  12.     for (i = 0; open_brackets[i]; ++i) {
  13.     if (bra == open_brackets[i]) {
  14.         return close_brackets[i];
  15.         break;
  16.     } /* if */
  17.     } /* for */
  18.     return 0;
  19. } /* match_of */
  20.  
  21.  
  22. char * find_match (char *text)
  23. {
  24.     char bra  = *text, curr, ket;
  25.     int stack = 0;
  26.  
  27.     ket = match_of(bra);             // get the close bracket
  28.  
  29.     while ((curr = *(++text)) && (stack || (curr != ket))) {
  30.     if (IS_ESC(curr))
  31.         if (!(curr = *(++text))) // ignore escaped characters
  32.         break;
  33.     else if (curr == bra)        // count open-brackets
  34.         ++stack;
  35.     else if (curr == ket)        // release them on close
  36.         --stack;
  37.     } /* while */
  38.  
  39.     if (curr)
  40.     return text;
  41.     return NULL;
  42. } /* find_match */
  43.  
  44.  
  45. /* SLOW! */
  46. char * strip_escs (char * text)
  47. {
  48.     int len = strlen (text);
  49.     char *dest = text, *ret = text;
  50.  
  51.     *(text + len - 1) = 0;  // remove the last
  52.     //REMOVE ();              // remove the first
  53.     ++text;
  54.  
  55.     while (c = *(text)) {
  56.     if (c == '\\') {
  57.         //REMOVE();       // remove the escape
  58.         ++text;
  59.         if (!(c = *text))
  60.         return NULL;
  61.     } /* if */
  62.     //ACCEPT();           // accept the character
  63.     *(dest++) = c; ++text;
  64.     } /* while */
  65.  
  66.     return ret;         // return the stripped string
  67. } /* strip_escs */
  68.  
  69.  
  70.